package com.sv.jm.baselibrary.Coustom;


import com.sv.jm.baselibrary.base.BaseActivity;
import com.sv.jm.baselibrary.response.BaseRespone;
import com.sv.jm.baselibrary.utils.ToastHelper;

import rx.Observer;

/**
 * Created by 胡金辉 on 2017/5/24.
 * <p>
 * 自定义观察者
 */

public abstract class HttpObserver implements Observer<BaseRespone> {
    BaseActivity bac;

    public HttpObserver(BaseActivity bac) {
        this.bac = bac;
    }

    @Override
    public void onCompleted() {
        bac.hideLoading();
    }

    @Override
    public void onError(Throwable e) {
        bac.hideLoading();
        ToastHelper.ToastShow(bac, e.getMessage());
    }

    @Override
    public void onNext(BaseRespone baseRespone) {
        onCompleted();
        int code = baseRespone.getCode();
        if (code == 0) {//失败
            ToastHelper.ToastShow(bac, baseRespone.getMsg());
        } else if (code == 1) {//成功
            onResult(baseRespone);
        }
    }

    /**
     * 之关注成功之后的返回结果  其余逻辑在noNext处理
     *
     * @param baseRespone
     */
    public abstract void onResult(BaseRespone baseRespone);
}
